package logViewer;

import java.io.InputStream;
import java.io.IOException;
import java.io.PrintStream;

import de.mud.telnet.TelnetWrapper;

public class TelnetClient {
	public static void main(String[] av) {
		String host = "172.18.10.77";
		int port = 23;
		switch (av.length) {
		case 2:
			port = Integer.parseInt(av[1]);
		case 1:
			host = av[0];
		case 0:
			break;
		default:
			System.exit(-1);
		}

		new TelnetClient(System.in, System.out, host, port);
	}

	public TelnetClient(InputStream in, PrintStream out, String host, int port) {
		TelnetWrapper telnetWrapper = new TelnetWrapper();
		try {
			telnetWrapper.connect(host, port);
		} catch (IOException e) {
			System.out.println("TelnetClient: Got exception during connect: "
					+ e);
			e.printStackTrace();
		}
		createAndStartReader(telnetWrapper, out);
		byte[] buf = new byte[256];
		int n = 0;
		try {
			while (n >= 0) {
				n = in.read(buf);
				if (n > 0) {
					byte[] sendBuf = new byte[n];
					System.arraycopy(buf, 0, sendBuf, 0, n);
					// Must be transpose (not send) or connect is closed.
					telnetWrapper.getHandler().transpose(sendBuf);
				}
			}
		} catch (IOException e) {
			System.out
					.println("TelnetClient: Got exception in read/write loop: "
							+ e);
			e.printStackTrace();
			return;
		} finally {
			try {
				telnetWrapper.disconnect();
			} catch (IOException e) {
				System.out
						.println("TelnetClient: got exception in disconnect: "
								+ e);
				e.printStackTrace();
			}
		}
	}

	class ReaderThread extends Thread {
		TelnetWrapper telnetWrapper;
		PrintStream out;

		ReaderThread(TelnetWrapper telnetWrapper, PrintStream out) {
			super("TelnetReaderThread");
			this.telnetWrapper = telnetWrapper;
			this.out = out;
		}

		public void run() {
			System.out.println("thread-starting");
			byte[] buf = new byte[256];
			int n = 0;
			while (n >= 0) {
				try {
					n = telnetWrapper.read(buf);
					if (n > 0) {
						out.print(new String(buf, 0, n));
					}
				} catch (IOException e) {
					System.out
							.println("ReaderThread.run: got exception in read/write loop: "
									+ e);
					e.printStackTrace();
					return;
				}
			}
		}
	}

	public Thread createAndStartReader(TelnetWrapper telnetWrapper,
			PrintStream out) {
		ReaderThread readerThread = new ReaderThread(telnetWrapper, out);
		readerThread.start();
		return readerThread;
	}
}

// End of file.